<?php

!defined('IN_NOVA') && exit('Access Denied!');

class Rss {

	public function __construct() {}

	public function __destruct() {}

	public function start() {
		global $request,$cache,$db;
		$cate_id = $request->p;
		$blog_url = $cache->config['domain'];
		$title = $cache->config['blogname'];
		$description = $cache->config['description'];
		$type = 'all';
		$url = $blog_url = substr( $blog_url,0,-1 ) . APP_PATH;

		if ( array_key_exists( $cate_id , $cache->category ) ) {
			$title = $cache->category[$cate_id]['name'] . ' - ' . $title;
			$description = $cache->category[$cate_id]['description'];
			//$url .=  U( '', 'category', $cache->category[$cate_id]['alias'] );
			$sql = "SELECT A.id,A.title,A.time,A.author,A.category,A.tag,A.summary,A.content,A.read,A.comment,A.type,A.address,A.update,C.name,C.description FROM `" . DB_PREFIX . "article` A JOIN `" . DB_PREFIX . "category` C ON A.category = C.cid WHERE A.isdel = 0 AND A.category = " . $cate_id . " ORDER BY A.id DESC LIMIT 0,10";
		} else {
			$sql = "SELECT A.id,A.title,A.time,A.author,A.category,A.tag,A.summary,A.content,A.read,A.comment,A.type,A.address,A.update,C.name,C.description FROM `" . DB_PREFIX . "article` A JOIN `" . DB_PREFIX . "category` C ON A.category = C.cid WHERE A.isdel = 0 ORDER BY A.id DESC LIMIT 0,10";
		}

		$rssdata = array();
		$query = $db->query($sql);
		while ($row = $db->fetch_array($query)) {
			if( $row['type'] == '动态' ){
				$address = $blog_url . 'read.php?id=' . $row['id'];
			} else {
				$address = $blog_url . 'article/' . $cache->category[$row['category']]['alias'] . '/' . $row['address'] . '.html';
			}
			$rssdata[] = array(
				'title' => $row['title'],
				'author' => $row['author'],
				'address' => $address,
				'description' => $row['content'],
				'category' => $row['name'],
				'pubDate' => gmdate( 'r', $row['time'] ),
			);
		}
		unset($sql,$query,$row,$address);

		echo '<?'.'xml version="1.0" encoding="utf-8"?'.'>',"\n";
		?>
		<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/commentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" >
		<channel>
			<title><?php echo $title ?></title>
			<link><?php echo $url ?></link>
			<description><?php echo $description ?></description>
			<language>zh-cn</language>
			<copyright>Powered By XKLog V<?php echo XKLOG_VERSION ?>.Copyright <?php echo gmdate( 'Y', PHP_TIME + 8 * 3600 ); ?> <?php echo $cache->config['blogname'] ?> All Rights Reserved.</copyright>
			<pubDate><?php echo gmdate( 'r', PHP_TIME + 8 * 3600 ); ?></pubDate>
			<generator>XKLog V<?php echo XKLOG_VERSION; ?></generator>
			<?php
			foreach ($rssdata as $rss) {
			?>

			<item>
				<title><![CDATA[<?php echo $rss['title']?>]]></title>
				<link><?php echo $rss['address']; ?></link>
				<author><?php echo $rss['author']; ?></author>
				<description><![CDATA[<?php echo $rss['description']; ?>]]></description>
				<category><?php echo $rss['category']; ?></category>
				<guid><?php echo $rss['address']; ?></guid>
				<pubDate><?php echo $rss['pubDate']; ?></pubDate>
			</item>

			<?php
			}
			?>
		</channel>
		</rss>
<?php
	}

}
?>